package com.aaa.car.service.impl;

import com.aaa.car.entity.CarType;
import com.aaa.car.entity.Shop;
import com.aaa.car.mapper.ShopMapper;
import com.aaa.car.service.ShopService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Shop)表服务实现类
 *
 * @author YuXin
 * @since 2020-11-19 19:37:57
 */
@Service("shopService")
public class ShopServiceImpl implements ShopService {
    @Resource
    private ShopMapper shopMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Shop queryById(Integer id) {
        return this.shopMapper.queryById(id);
    }

    /**
     * 查询
     *
     * @param
     * @param
     * @return 对象列表
     */
    @Override
    public Result queryAllByParam(Integer pageNo, Integer pageSize, Shop shop) {
        if (pageNo == 0 || pageSize == 0) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Shop> pageInfo = new PageInfo<>(shopMapper.queryAll(shop));
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), pageInfo);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param shop 实例对象
     * @return 对象列表
     */
    @Override
    public List<Shop> queryAll(Shop shop) {
        return this.shopMapper.queryAll(shop);
    }

    /**
     * 新增数据
     *
     * @param shop 实例对象
     * @return 实例对象
     */
    @Override
    public Shop insert(Shop shop) {
        this.shopMapper.insert(shop);
        return shop;
    }

    /**
     * 修改数据
     *
     * @param shop 实例对象
     * @return 实例对象
     */
    @Override
    public Shop update(Shop shop) {
        this.shopMapper.update(shop);
        return this.queryById(shop.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.shopMapper.deleteById(id) > 0;
    }
}